Skip to content

fix(functions): serve the other functions when one fails to load, and answer its route with a 502 - #329

Merged
chhavi-mandowara-cstk merged 1 commit into
v2-devfrom
CL-8970
Dec 4, 2026
Merged

chhavi-mandowara-cstk merged 1 commit into
v2-devfrom
CL-8970

Conversation

@chhavi-mandowara-cstk

Copy link
Copy Markdown
Contributor

What was wrong

A function file that throws while it loads (for example if (!process.env.X) throw … at the top), or that fails to bundle, made launch:functions:serve exit 1 with Could not load functions/<file>.js: …. Every other function was then unreachable locally.

Deployed, it depends on the cloud:

Where One bad function file Why
AWS Only that route answers 502. Every other function keeps working. The deployment agent deploys each function file as its own Lambda (package: individually: true, one handler per file in setup_cloud_functions.go). Only the bad file's Lambda fails to start.
Azure / GCP (CLI 1.6.0) Every route answers 500 CF001 ("Server failed to start"). Every function runs in one launch-cli server. 1.6.0 throws while loading and the server never starts, so the launcher answers CF001.
serve on v2-dev Serve exits 1, so nothing is served. loadCloudFunctionResources throws FunctionLoadError for the first file that fails.

This is CL-8970 gap 9.

The change (src/functions/cloud-functions.ts)

  • A file that fails to load no longer stops serve. Its FunctionLoadError message is logged instead: the same Could not load functions/<file>.js: <cause> (<file>:<line>:<column>) as before. It is logged when serve starts, and again on each request to that route, so a 502 always comes with its reason.
  • That route answers a bare 502, as its Lambda does on AWS. A dynamic route (items/[id].js) keeps its shape.
  • Every other function serves as before.
  • A package that is not installed still stops serve with MissingPackageError ("… Run npm install in the project"). That is the local install, not the function's code; on AWS the deploy installs packages.
  • AGENTS.md describes the new behaviour and what Azure/GCP (V1) do.
  • .talismanrc: refreshed checksums for cloud-functions.test.ts and AGENTS.md (test code and prose, no credentials).

Tests

src/functions/cloud-functions.test.ts:

  • New: serves the other functions when one throws while loading, and answers its route with a bare 502: /ok gives 200 ok, /broken a bare 502, and the reason is logged.
  • New: logs why a route answers 502 when serve starts and again on each request to it: logged once at start, then once per request.
  • New: answers a 502 on a dynamic route whose function fails to load, keeping its path parameter.
  • The six tests that expected serve to refuse now pin the same messages and locations from the log, plus the 502:
    • syntax error with file:line:col;
    • thrown error with file:line:col;
    • thrown non-error value;
    • cannot be bundled;
    • nested api/boom.js;
    • unresolvable relative import.
  • The MissingPackageError tests are unchanged and still pass.

Proof:

  • Against v2-dev, the new tests fail: serve rejects with FunctionLoadError.
  • Each line is proven by a mutant:
    • dropping the per-request log fails the log test;
    • dropping the startup log fails the log test;
    • answering 500 instead of 502 fails 6 tests.

Full suite in the pre-push hook: 189 suites, 3847 tests passed. Every hook ran (Snyk included), with no --no-verify.

E2E (http-sanity cloud-functions checks, AWS expectations)

Gap 9 can't go into the shared 30-check site: on Azure/GCP one bad file would make every check answer 500. So e2e PR contentstack/contentfly-e2e-tests#503 adds a separate small site (ok.js, and a broken.js that throws while loading). It is deployed to its own environment, behind the same LAUNCH_E2E_CLOUD_FUNCTION_PARITY switch.

Against launch:functions:serve:

Site v2-dev (5eb03c8) This PR
load-failure site (2 checks) 0/2: serve exits 1 (Could not load functions/broken.js: …) 2/2: /ok 200 ok, /broken 502
main site (30 checks) 29/30 29/30, no check got worse (the one failing is gap 10, require/__dirname)

Not changed

  • A function file without a default-export function is still skipped (no route, so 404), as before.
  • Missing packages still stop serve, as above.
  • The startup list (Detected Serverless Functions) still lists the failed route; the logged message above it says why it answers 502.

Jira: https://contentstack.atlassian.net/browse/CL-8970


Generated by Claude Code

… answer its route with a 502

Each deployed AWS function is its own Lambda, so a file that throws while
loading (or fails to bundle) fails only its own route, with a 502, and
every other function keeps working. Serve stopped with FunctionLoadError
instead, so one half-finished function made every other function
unreachable locally.

Serve now logs the same "Could not load functions/<file>.js: <cause>"
message, with the file, line and column, when it starts and on each
request to that route, and the route answers a bare 502. Every other
function serves as before. A package that is not installed still stops
serve with MissingPackageError, since that is the local install, not the
function's code.

The tests that expected serve to refuse now pin the same messages and
locations from the log, and the 502. AGENTS.md describes the change, and
the .talismanrc checksums of the two touched files are refreshed.
@chhavi-mandowara-cstk
chhavi-mandowara-cstk requested a review from a team as a code owner December 4, 2026 13:10
@snyk-io

snyk-io Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

🔒 Security Scan Results

ℹ️ Note: Only vulnerabilities with available fixes (upgrades or patches) are counted toward thresholds.

Check Type Count (with fixes) Without fixes Threshold Result
🔴 Critical Severity 0 0 10 ✅ Passed
🟠 High Severity 0 2 25 ✅ Passed
🟡 Medium Severity 0 10 500 ✅ Passed
🔵 Low Severity 0 0 1000 ✅ Passed

⏱️ SLA Breach Summary

✅ No SLA breaches detected. All vulnerabilities are within acceptable time thresholds.

Severity Breaches (with fixes) Breaches (no fixes) SLA Threshold (with/no fixes) Status
🔴 Critical 0 0 15 / 30 days ✅ Passed
🟠 High 0 0 30 / 120 days ✅ Passed
🟡 Medium 0 0 90 / 365 days ✅ Passed
🔵 Low 0 0 180 / 365 days ✅ Passed

ℹ️ Vulnerabilities Without Available Fixes (Informational Only)

The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:

  • Critical without fixes: 0
  • High without fixes: 2
  • Medium without fixes: 10
  • Low without fixes: 0

✅ BUILD PASSED - All security checks passed

@chhavi-mandowara-cstk
chhavi-mandowara-cstk merged commit 91c1d72 into v2-dev Dec 4, 2026
8 checks passed
@chhavi-mandowara-cstk
chhavi-mandowara-cstk deleted the CL-8970 branch December 4, 2026 15:17
tejas-contentstack pushed a commit that referenced this pull request Dec 4, 2026
Brings in the functions fixes from #327, #328 and #329. The only
conflict was .talismanrc, the AGENTS.md checksum line: both sides had
changed it, so it is recomputed with Talisman for the merged AGENTS.md.
All of our other .talismanrc entries are kept as they were.

#claude_code#
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants